package org.lanqiao.service.impl;

import org.lanqiao.dao.*;
import org.lanqiao.entity.*;
import org.lanqiao.service.WxuserGiftService;
import org.lanqiao.util.DataNow;
import org.lanqiao.util.Result;
import org.lanqiao.util.ResultUtil;
import org.lanqiao.util.UUId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Date;

@Service
public class WxuserGifServiceImpl implements WxuserGiftService {

    @Autowired
    WxuserGiftDao wxuserGiftDao;
    @Autowired
    ActivityDao activityDao;
    @Autowired
    WxuserDao wxuserDao;
    @Autowired
    UserDao userDao;
    @Autowired
    AdminDao adminDao;


    @Override
    public Result addGift(String wxid, String acid, String income) {
        Wxuser wxUser = wxuserDao.getWxUser(wxid);
        double wxBalance = (double) wxUser.getWxBalance() - Double.parseDouble(income);
//        判断余额是否充足
        if (wxBalance < 0) {
            return Result.defeated("余额不足");
        } else {
//         修改微信用户余额
            wxuserDao.updateNumber(wxid, wxBalance);
//        获取分钱比列
            Admin www = adminDao.getAdmin("www", "123");
            double adminPercent = Double.parseDouble(www.getAdminPercent());
//            超管获取的钱
            double minBalance = Integer.parseInt(income) * adminPercent;
//            发布者获取的钱
            double userBalance = Integer.parseInt(income) - minBalance;
            String adminBalance = www.getAdminBalance();
            adminDao.updateNumber("1",minBalance+Double.parseDouble(adminBalance)+"");
//            根据活动id获取活动
            Activity activity = activityDao.getActivityById(acid);
//            获取发布者id
            String userId = activity.getUserId();
            User user = userDao.getUser(userId);
//            修改发布者钱
            userDao.updateNumber(userId,userBalance+user.getUserBalance());
            String data = DataNow.getData();
            Integer integer = new Integer(1);
            Double dou = Double.parseDouble(income)+activity.getActivityIncome();
            activityDao.saveMoney(acid, dou);
            WxuserGift wg = new WxuserGift(UUId.getUUID(), wxid, acid, data, integer, dou);
            wxuserGiftDao.addGift(wg);
            return Result.ok(null);
        }
    }
}
